package com.sdry.approval.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.sdry.approval.constant.ApprovalTypeEnum;
import com.sdry.approval.entity.LeaveMan;
import com.sdry.approval.entity.LeaveRequest;
import com.sdry.approval.mapper.LeaveRequestMapper;
import com.sdry.untils.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @program: info_sdry
 * @description: 请假
 * @author: cb
 * @create: 2020-02-21 16:02
 **/
@Service
@Transactional(rollbackFor = Exception.class)
public class LeaveRequestServiceImpl extends ServiceImpl<LeaveRequestMapper,LeaveRequest> implements LeaveRequestService {
    @Autowired
    private LeaveRequestMapper leaveRequestMapper;


    @Autowired
    private MeSponsorService meSponsorService;

    @Autowired
    private ForMySponsorService forMySponsorService;

    @Override
    public boolean add(LeaveRequest leaveRequest, String[] userId) {
        leaveRequest.setCreateBy(UserContext.getSessionUserIdByServiceImpl().toString());
        Integer effect = leaveRequestMapper.insert(leaveRequest);
        if (userId != null){
            for (String str : userId) {
                leaveRequestMapper.addLeaveMan(str,leaveRequest.getLeaveRequestId(),"sp","qj");
            }

        }
        if (leaveRequest.getCopyGiveBy() != null){
            leaveRequestMapper.addLeaveMan(leaveRequest.getCopyGiveBy().toString(),leaveRequest.getLeaveRequestId(),"cs","qj");
        }

        if (effect == 0){
            return false;
        }else {
            //我发起的
            meSponsorService.addMeSponsor(ApprovalTypeEnum.TYPE_APPROVAL_QJ.getCode(),leaveRequest.getLeaveRequestId());
            return true;
        }

    }

    @Override
    public List<LeaveMan> queryLeaveManList(Long requestId, String requestType) {
        return leaveRequestMapper.queryLeaveManList(requestId,requestType);
    }

    /**
     * 审批
     * @param requertType 审批类型
     * @param requestId 审批ID
     * @param proposal
     * @return
     */
    @Override
    public boolean approval(String requertType, Long requestId, String proposal) {
        //添加意见或建议
        Integer afect = leaveRequestMapper.approval(requertType,requestId,proposal,UserContext.getSessionUserIdByServiceImpl());
        if (afect == 0){
            return false;
        }
        //待我审核的状态
        Integer effect = forMySponsorService.updateForMy(requertType,requestId,UserContext.getSessionUserIdByServiceImpl(),"1");
        if (effect == 0){
            return false;
        }
        //我发起的状态
        /**
         * 是否是最后一个人审批
         */
        Integer affect = 0;
        List<LeaveMan> leaveManList = leaveRequestMapper.queryLeaveManList(requestId, requertType);
        if (leaveManList.size() > 0) {
            if (UserContext.getSessionUserIdByServiceImpl().equals(leaveManList.get(leaveManList.size() -1 ).getUserId())){

                affect = meSponsorService.updateMeSponor(requertType,requestId,"tg");
            }
        }
        if (affect == 0){
            return false;
        }
        return true;
    }
}
